package us.clubadvocate.server;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

import us.clubadvocate.client.index.ClubRpcService;
import us.clubadvocate.shared.Pet;

import com.google.appengine.api.utils.SystemProperty;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ClubRpcServiceImpl extends RemoteServiceServlet implements ClubRpcService
{
  private static final long serialVersionUID = 4373224220176186820L;
  static final Logger log = Logger.getLogger(ClubRpcServiceImpl.class.getName());

  static final String SQL_DB_URL = "jdbc:mysql://localhost/menagerie";
  static final String SEVER_DB_URL = "jdbc:google:mysql://sceadumind:club-advocator-sql-1/menagerie";
  
  // ---------------------------------------------------------------------------
  // ---------------------------------------------------------------------------
  
  static Connection getConnection() throws SQLException
  {
    if(SystemProperty.environment.value() == SystemProperty.Environment.Value.Production)
    {
      try
      {
        Class.forName("com.mysql.jdbc.GoogleDriver");
      }
      catch(ClassNotFoundException e)
      {
        printStackTrace(e);
      }
      return DriverManager.getConnection(SEVER_DB_URL, "agent8261", "gold3608");
    }
    return DriverManager.getConnection(SQL_DB_URL, "root", "password");
  }
  
  // ---------------------------------------------------------------------------
  
  static void closeConnection(Connection conn)
  {
    if(conn != null)
    {
      try
      { conn.close(); }
      catch(SQLException e)
      { printStackTrace(e); }
    }
  }
  
  // ---------------------------------------------------------------------------
  
  @Override
  public Pet getRecord(String key)
  {
    Pet p = null;
    Connection conn = null;
    try
    {
      conn = getConnection();
      StringBuilder b = new StringBuilder("select * from pet where name=");
      b.append("\'").append(key).append("\'");
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(b.toString());
      if(rs.first())
      {
        p = new Pet();
        p.name = rs.getString(1); p.owner = rs.getString(2);
        p.species = rs.getString(3); p.sex = rs.getString(4);
        p.birth = (rs.getDate(5) == null) ? null : new java.util.Date(rs.getDate(5).getTime());
        p.death = (rs.getDate(6) == null) ? null : new java.util.Date(rs.getDate(6).getTime());
      }
    }
    catch(SQLException e)
    {
      printStackTrace(e);
    }
    closeConnection(conn);
    return p;
  }
  
  // ---------------------------------------------------------------------------
  
  public static void printStackTrace(Throwable t)
  {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    PrintStream p = new PrintStream(b);
    t.printStackTrace(p);
    log.severe(new String(b.toByteArray()));
  }
  // ---------------------------------------------------------------------------
}
